﻿//*** Guidance generated code ***//
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;

namespace $safeprojectname$.Binding
{
    public class ContainerToProvider : IValueProvider
    {
        protected readonly IValueContainer ValueContainer;
        public ContainerToProvider(IValueContainer valueContainer)
        {
            ValueContainer = valueContainer;
        }

        public bool ContainsPrefix(string prefix)
        {
            return ValueContainer.ContainsPrefix(prefix);
        }

        public ValueProviderResult GetValue(string key)
        {
            var result = ValueContainer.GetValue(key);
            if (result.IsNull())
            {
                return null;
            }

            var rawValue = result.RawValue;

            // API rawValue for multi is IList<> while
            // MVC prefers Array[]
            var list = rawValue as IEnumerable<object>;
            if (list.Is())
            {
                rawValue = list.ToArray(); // MVC binder will be able to continue
            }

            return new ValueProviderResult(rawValue, result.AttemptedValue, result.Culture);
        }
    }
}